package demo.bocweb.login_module.activity;

import android.os.CountDownTimer;
import android.text.InputType;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import demo.bocweb.com.sdk.baseApi.RequestHelper;
import demo.bocweb.com.sdk.config.ARoutConfig;
import demo.bocweb.com.sdk.config.AppConfig;
import demo.bocweb.com.sdk.manager.MessageEvent;
import demo.bocweb.com.sdk.util.Loading;
import demo.bocweb.com.sdk.util.MD5Utils;
import demo.bocweb.com.sdk.util.SP;
import demo.bocweb.com.sdk.util.StringUtils;
import demo.bocweb.com.sdk.util.ToastyUtil;
import demo.bocweb.login_module.base.BaseRegisterActivity;
import demo.bocweb.login_module.bean.LoginBean;
import demo.bocweb.user_module.R;
import demo.bocweb.user_module.R2;
import okhttp3.RequestBody;

/**
 * 注册
 * Created by zhelun on 2018/1/26.
 */
@Route(path = ARoutConfig.registerPath)
public class RegisterActivity extends BaseRegisterActivity {
    @BindView(R2.id.close)
    ImageView close;
    @BindView(R2.id.logo)
    ImageView logo;
    @BindView(R2.id.phone)
    EditText phone;
    @BindView(R2.id.code)
    EditText code;
    @BindView(R2.id.send)
    TextView send;
    @BindView(R2.id.psw)
    EditText psw;
    @BindView(R2.id.eye)
    ImageView eye;
    @BindView(R2.id.yaoqingCode)
    EditText yaoqingCode;
    @BindView(R2.id.registe)
    TextView registe;
    @BindView(R2.id.registCB)
    CheckBox registCB;
    @BindView(R2.id.login)
    TextView login;
    private CountDownTimer timer;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public void register() {
        if (StringUtils.isEmpty(phone.getText().toString().trim())) {
            ToastyUtil.showWarning("手机号不能为空");
            return;
        }
        if (!StringUtils.isPhoneNumberValid(phone.getText().toString().trim())) {
            ToastyUtil.showWarning("非法的手机号");
            return;
        }
        if (StringUtils.isEmpty(code.getText().toString())) {
            ToastyUtil.showWarning("验证码不能为空");
            return;
        }
        if (StringUtils.isEmpty(psw.getText().toString())) {
            ToastyUtil.showWarning("密码不能为空");
            return;
        }

        Loading.show(this, getString(R.string.str_loading));
        RequestBody body = RequestHelper.build()
                .add("Phone", phone.getText().toString())
                .add("Password", MD5Utils.getMD5(psw.getText().toString()))
                .add("SmsCode", code.getText().toString())
                .create();
        mPresenter.register(body);
    }

    @Override
    public void checkSmsCode() {
        if (StringUtils.isEmpty(phone.getText().toString().trim())) {
            ToastyUtil.showWarning("手机号不能为空");
            return;
        }
        if (!StringUtils.isPhoneNumberValid(phone.getText().toString().trim())) {
            ToastyUtil.showWarning("非法的手机号");
            return;
        }
        if (StringUtils.isEmpty(code.getText().toString())) {
            ToastyUtil.showWarning("验证码不能为空");
            return;
        }
        RequestBody body = RequestHelper.build()
                .add("Phone", phone.getText().toString())
                .add("Type", 0)
                .add("Sms", code.getText().toString())
                .create();
        mPresenter.checkSmsCode(body);
    }

    @Override
    public void sendSmsCode() {
        if (StringUtils.isEmpty(phone.getText().toString().trim())) {
            ToastyUtil.showWarning("手机号不能为空");
            return;
        }
        if (!StringUtils.isPhoneNumberValid(phone.getText().toString().trim())) {
            ToastyUtil.showWarning("非法的手机号");
            return;
        }
        RequestBody body = RequestHelper.build()
                .add("Phone", phone.getText().toString())
                .add("Type", 0)
                .create();
        mPresenter.sendSmsCode(body);
    }


    @Override
    public void registerSuccess(LoginBean bean) {
        ToastyUtil.showSuccess("注册成功");
        if (!AppConfig.registOkIsNeedLogin) {
            SP.put(AppConfig.Authorization, bean.getToken());
            EventBus.getDefault().post(new MessageEvent("registOk"));
        }
        finish();
    }

    @Override
    public void sendSmsCodeSuccess() {
        timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                send.setText(l / 1000 + "s");
                send.setClickable(false);
            }

            @Override
            public void onFinish() {
                send.setText("重新获取");
                send.setClickable(true);
            }
        }.start();
    }

    @Override
    public void checkSmsSuccess() {
        register();
    }


    @OnClick({R2.id.close, R2.id.send, R2.id.eye, R2.id.registe, R2.id.login})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.close) {
            finish();
        } else if (i == R.id.send) {
            sendSmsCode();

        } else if (i == R.id.eye) {
            int inputType = psw.getInputType();
            if (inputType == (InputType.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD)) {
                eye.setImageResource(R.mipmap.ic_login_eye_open);
                psw.setInputType(InputType.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            } else {
                eye.setImageResource(R.mipmap.ic_login_eye_close);
                psw.setInputType(InputType.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
            }
            psw.setSelection(psw.getText().toString().length());

        } else if (i == R.id.registe) {
            if (AppConfig.isNeedCheckSmsCode) {
                checkSmsCode();
            } else {
                register();
            }


        } else if (i == R.id.login) {
            finish();

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }

}
